<?php
/**
 * Created by PhpStorm.
 * User: 聪
 * Date: 2019/4/18
 * Time: 2:57
 */

namespace app\front\logic;

use app\front\model\User;
use app\shared\model\Sms;

class UserLogic extends Base
{
    //用户数据加密的KET;
    CONST TOKEN_KEY = 'user_info';
    //未认证状态值;
    CONST NOT_ATTESTATION = 2;
    protected static $status = [0 => '未上传资料', 1 => '待审核', 2 => '审核成功', 3 => '审核不通过'];

    public static function get_info(array $params): string
    {
        $user = new User();
        $info = $user->getOne(['id' => $params['front_user']['uid']]);
        $ret  = [
            'uid'         => $params['front_user']['uid'],
            'avatar'      => $info['avatar'],
            'nickname'    => $info['nickname'],
            'mobile'      => $info['mobile'],
            'attestation' => $info['attestation'] == 2 ? false : true,
            'status'      => 0,
            'statusTxt'   => self::$status[0],
            'remark'      => '',
        ];
        if (!empty($info->UserCredentials)) {
            $ret['status']    = $info->UserCredentials->status;
            $ret['statusTxt'] = self::$status[$info->UserCredentials->status];
            $ret['remark']    = $info->UserCredentials->remark;
        }
        return static::returnJson($ret);
    }

    public static function update(array $param): string
    {
        $user = new User();
        $data = [];
        //更新手机号；
        if (isset($param['mobile'])) {
            $data['mobile'] = $param['mobile'];
            if ($user->getValByCol($data, 'mobile')) return static::returnError(20001);
            $sms = new Sms();
            $ret = $sms->getOne([
                'msg' => $param['sms'],
                'uid' => $param['front_user']['uid'],
            ], [], 'id DESC')->toArray();
            if ($ret['etime'] < time() || empty($ret)) {
                return static::returnError(20008);
            }
        }
        //更新昵称；
        if (isset($param['nickname'])) {
            $data['nickname'] = $param['nickname'];
        }
        return static::returnJson($user->saveOne($data, ['id' => $param['front_user']['uid']]), 10003, 20004);
    }


    public static function saveCredentials(array $param): string
    {
        $model = new UserCredentials();
        $ret   = $model->findOrCreate(['uid' => $param['front_user']['uid']], [
            'positive' => $param['positive'],
            'back'     => $param['back'],
            'license'  => $param['license'],
            'uid'      => $param['front_user']['uid'],
        ]);
        return is_object($ret) ? static::returnError(20009) : static::returnJson(true, 10009);
    }

    public static function registered(array $param): string
    {
        $applets = new \applets\User();
        $info    = $applets->get_session($param['js_code']);

        if (isset($info['errcode'])) {
            var_dump($info);
            return static::returnError(20002);
        }

        $user       = new User();
        $ret        = null;
        $createData = [
            'open_id' => $info['openid'],
        ];
        if (!empty($param['info']['userInfo'])) {
            $saveData['nickname']    = $param['info']['userInfo']['nickName'];
            $saveData['avatar']      = $param['info']['userInfo']['avatarUrl'];
            $saveData['attestation'] = 1;
            if ($user->saveOne($saveData, $createData)) {
                $ret = $user->getOne($createData);
            };
        } else {
            $ret = $user->findOrCreate(['open_id' => $info['openid']], $createData);
        }
        $hour     = 2;
        $userInfo = [
            'token'        => encrypt(['uid' => $ret['id'],], static::TOKEN_KEY, $hour),
            'is_authorize' => (bool)$ret['avatar']
        ];
        return static::returnJson($userInfo, 10001);
    }
}